package com.hg.miaosha.repository;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hg.miaosha.dao.SeckillDao;
import com.hg.miaosha.dto.PageDTO;
import com.hg.miaosha.entity.Seckill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * created by skh on 2019/6/15
 */
@Repository
public class SeckillRepositoryImpl implements SeckillRepository {

	@Autowired
	private SeckillDao seckillDao;

	@Override
	public Page getSeckillGoodsList(PageDTO pageDTO) {
		Page<Seckill> page = pageDTO.pageInit(pageDTO);
		List<Seckill> seckillList = seckillDao.selectPage(page, new EntityWrapper<>());
		page.setRecords(seckillList);
		return page;
	}

	@Override
	public Seckill getGoodsStockBySeckillId(Integer seckillId) {

		Seckill wrapper = new Seckill();
		wrapper.setSeckillId(seckillId);

		return seckillDao.selectOne(wrapper);

	}

	@Override
	public void decreaseStockBySeckillId(Integer seckillId) {
		seckillDao.decreaseStockBySeckillId(seckillId);
	}

	@Override
	public List<Seckill> getAllSeckillGoodsList() {
		return seckillDao.selectList(null);
	}

	@Override
	public void updateSeckillById(Seckill seckill) {
		seckillDao.updateById(seckill);
	}

	@Override
	public Seckill getGoodsStockBySeckillIdWithPessimisticLock(Integer seckillId) {
		return seckillDao.getGoodsStockBySeckillIdWithPessimisticLock(seckillId);
	}

    @Override
    public Integer updateSeckillNumberBySecKillId(Integer seckillId) {
		return seckillDao.updateSeckillNumberBySecKillId(seckillId);
    }

	@Override
	public Integer updateSeckillNumberBySecKillIdWithOptimisticLock(Integer seckillId, Integer version) {
		return seckillDao.updateSeckillNumberBySecKillIdWithOptimisticLock(seckillId, version);
	}
}
